<?php


namespace App\Server;


use Illuminate\Support\Facades\Cache;

class Token
{
    protected static $tll = 3600;

    /**
     * 创建token
     * @param $id
     * @param string $key
     * @return string
     */
    public static function create($id, $key = '')
    {
        if (!$key) {
            $key = uniqid();
        }
        $token = md5($key);
        Cache::add($token, $id, self::$tll);
        return $token;
    }

    /**
     * 通过token获取用户id
     * @param $token
     * @return bool|mixed
     */
    public static function check($token)
    {
        if (!$token) {
            return false;
        }
        if (!Cache::has($token)) {
            return false;
        }
        return Cache::get($token);
    }


    /**
     * 更新过期时间
     * @param $token
     * @param $id
     */
    public static function delayed($token, $id)
    {
        Cache::put($token, $id, self::$tll);
    }
}